package com.example.wanjialei.service.serviceimpl;

import com.example.wanjialei.mapper.UserMapper;
import com.example.wanjialei.pojo.User;
import com.example.wanjialei.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    //查所有
    @Override
    public Map<String, Object> getUserInfo(User user) {
        Map<String, Object> map = new HashMap<>();
        List<User> users = userMapper.selectByExample(null);
        if (users.size() == 1) {
            map.put("flag", true);
            map.put("message", "查询成功");
            map.put("total", users.size());
            map.put("rows", users);
        } else {
            map.put("flag", false);
            map.put("message", "查询失败");
            map.put("total", users.size());
            map.put("rows", users);
        }
        return map;

    }

    //根据姓名查 姓名为主键 测试项目使用
    @Override
    public Map<String, Object> getUserInfoByName(User user) {
        Map<String, Object> map = new HashMap<>();
        User row = userMapper.selectByPrimaryKey(user.getUserName());
        if (row != null) {
            map.put("flag", true);
            map.put("message", "查询成功");
            map.put("total", 1);
            map.put("rows", row);
        } else {
            map.put("flag", false);
            map.put("message", "查询失败");
            map.put("total", 0);
            map.put("rows", row);
        }
        return map;
    }

    //用户登录功能
    @Override
    public Map<String, Object> getUserSign(User user) {
        Map<String, Object> map = new HashMap<>();
        User u = userMapper.selectByPrimaryKey(user.getUserName());
        if (u == null) {
            map.put("flag", false);
            map.put("message", "用户名不存在！");
            map.put("rows", u);
        } else {
            if (!u.getUserPassword().equals(user.getUserPassword())) {
                map.put("flag", false);
                map.put("message", "密码错误！");
            } else {
                map.put("flag", true);
                map.put("message", "登录成功！");
                map.put("rows", u);
            }
        }
        return map;
    }
}
